<?php

class SomeController extends BaseController {
	public function getIndex() {
		echo "welcome Mutha Fukha";
	}
    
    public function postCreatedatabase() {
		$returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Adding table on database success.";
        $isError          = FALSE;
        $missingParams    = null;

        if(!$isError){
            try {
                Schema::dropIfExists('users');
                Schema::dropIfExists('gangguan');
                Schema::dropIfExists('tindakan');
                Schema::dropIfExists('komponen');
                Schema::dropIfExists('penyulang');
                Schema::dropIfExists('locations');
                
                Schema::create('users', function($table){
                    $table->string('username');
                    $table->string('password');
                    $table->string('role');
                });
                Schema::create('gangguan', function($table){
                    $table->increments('id');
                    $table->dateTime('tanggal');
                    $table->string('area');
                    $table->string('rayon');
                    $table->string('feeder');
                    $table->text('lokasi')->nullable();
                    $table->double('longitude', 15, 12);
                    $table->double('latitude', 15, 12);
                    $table->string('nama_tiang');
                    $table->string('komponen');
                    $table->string('tindakan');
                    $table->string('foto');
                });
                Schema::create('tindakan', function($table){
                    $table->increments('id');
                    $table->string('tindakan');
                });
                Schema::create('komponen', function($table){
                    $table->increments('id');
                    $table->string('komponen');
                });
                Schema::create('penyulang', function($table){
                    $table->increments('id');
                    $table->dateTime('tanggal');
                    $table->string('area');
                    $table->string('rayon');
                    $table->string('feeder');
                    $table->string('nama_gardu');
                    $table->string('merk_trafo');
                    $table->text('lokasi');
                    $table->integer('datatrafo_kapasitas');
                    $table->decimal('datatrafo_arusnominal', 10, 2);
                    $table->integer('datapengukuran_arus_R_sumber');
                    $table->integer('datapengukuran_arus_R_kiri');
                    $table->integer('datapengukuran_arus_R_kanan');
                    $table->integer('datapengukuran_arus_S_sumber');
                    $table->integer('datapengukuran_arus_S_kiri');
                    $table->integer('datapengukuran_arus_S_kanan');
                    $table->integer('datapengukuran_arus_T_sumber');
                    $table->integer('datapengukuran_arus_T_kiri');
                    $table->integer('datapengukuran_arus_T_kanan');
                    $table->integer('datapengukuran_arus_N_sumber');
                    $table->integer('datapengukuran_arus_N_kiri');
                    $table->integer('datapengukuran_arus_N_kanan');
                    $table->integer('datapengukuran_tegangan_FN');
                    $table->integer('datapengukuran_tegangan_FF');
                    $table->integer('datapengukuran_tegangan_ujung_kanan');
                    $table->integer('datapengukuran_tegangan_ujung_kiri');
                    $table->decimal('beban_total_kVA', 10, 2);
                    $table->decimal('beban_total_percent', 10, 2);
                    $table->decimal('beban_fasa_tertinggi_kVA', 10, 2);
                    $table->decimal('beban_fasa_tertinggi_percent', 10, 2);
                    $table->decimal('kesetimbangan', 10, 2);
                    $table->decimal('inetral', 10, 2);
                    $table->decimal('ketidakseimbangan_sumber', 10, 2);
                    $table->decimal('ketidakseimbangan_kiri', 10, 2);
                    $table->decimal('ketidakseimbangan_kanan', 10, 2);
                });
                Schema::create('locations', function($table){
                    $table->increments('id');
                    $table->string('area');
                    $table->string('rayon');
                    $table->string('penyulang');
                });    
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode);
	}
    
    public function postAddkomponen() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Create dummy komponen success.";
        $isError          = FALSE;
        $missingParams    = null;

        if(!$isError){
            try {
                
                Komponen::truncate();
                
                $value = array('Tiang beton','Tiang besi','Kabel (SKTM)','Isolator','Lighting Arrester','Fuse Link','Holder c.o','Pentanahan L Arrester','LBS/ABS','DS/PMS','Jamperan TM','Skur','Travers/Cross Arm','Penghantar (SUTM)','Penghantar Hilang','Penghantar Kendor','Pelindung Penghantar','Pondasi tiang','Penegang tiang','Arm tie','Rampas-rampas','Lain-lain');
                
                for ($i = 0; $i < count($value); $i++) {
                    $komponen = new Komponen;
                    
                    $komponen->komponen = $value[$i];
                    
                    $komponen->save();
                }
                    
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode);
    }
    
    public function postAddtindakan() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Create dummy tindakan success.";
        $isError          = FALSE;
        $missingParams    = null;

        if(!$isError){
            try {
                
                Tindakan::truncate();
                
                $value = array('bersihkan', 'cat kembali', 'lumasi', 'potong pohon', 'rampas pohon', 'kencangkan ', 'tarik', 'stel kembali', 'tambah', 'test', 'perbaiki', 'ganti', 'pasang', 'geser ', 'bongkar');               
                
                for ($i = 0; $i < count($value); $i++) {
                    $tindakan   = new Tindakan;
                    
                    $tindakan->tindakan = $value[$i];
                    
                    $tindakan->save();
                }
                    
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode);
    }
    
    public function postAddpenyulangcolumn() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Add column in table penyulang success.";
        $isError          = FALSE;
        $missingParams    = null;

        if(!$isError){
            try {
                Schema::table('penyulang', function($table)
                {
                    $table->decimal('ketidakseimbangan_sumber', 10, 2);
                    $table->decimal('ketidakseimbangan_kiri', 10, 2);
                    $table->decimal('ketidakseimbangan_kanan', 10, 2);
                });
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode);   
    }
    
    public function postCompletepenyulang() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Completing dummy for table penyulang success.";
        $isError          = FALSE;
        $missingParams    = null;

        if(!$isError){
            try {
                foreach (Penyulang::all() as $penyulang) {
                    $ratarata_sumber = ($penyulang->datapengukuran_arus_R_sumber + $penyulang->datapengukuran_arus_S_sumber + $penyulang->datapengukuran_arus_T_sumber) / 3;
                    $ratarata_kiri   = ($penyulang->datapengukuran_arus_R_kiri + $penyulang->datapengukuran_arus_S_kiri + $penyulang->datapengukuran_arus_T_kiri) / 3;
                    $ratarata_kanan  = ($penyulang->datapengukuran_arus_R_kanan + $penyulang->datapengukuran_arus_S_kanan + $penyulang->datapengukuran_arus_T_kanan) / 3;
                    
                    $sumber_R   = $penyulang->datapengukuran_arus_R_sumber / $ratarata_sumber;
                    $sumber_S   = $penyulang->datapengukuran_arus_S_sumber / $ratarata_sumber;
                    $sumber_T   = $penyulang->datapengukuran_arus_T_sumber / $ratarata_sumber;
                    
                    $kiri_R     = $penyulang->datapengukuran_arus_R_kiri / $ratarata_kiri;
                    $kiri_S     = $penyulang->datapengukuran_arus_S_kiri / $ratarata_kiri;
                    $kiri_T     = $penyulang->datapengukuran_arus_T_kiri / $ratarata_kiri;
                    
                    $kanan_R    = $penyulang->datapengukuran_arus_R_kanan / $ratarata_kanan;
                    $kanan_S    = $penyulang->datapengukuran_arus_S_kanan / $ratarata_kanan;
                    $kanan_T    = $penyulang->datapengukuran_arus_T_kanan / $ratarata_kanan;
                    
                    $ketidakseimbangan_sumber = (abs($sumber_R - 1) + abs($sumber_S - 1) + abs($sumber_T - 1)) / 3; 
                    $ketidakseimbangan_kiri   = (abs($kiri_R - 1) + abs($kiri_S - 1) + abs($kiri_T - 1)) / 3; 
                    $ketidakseimbangan_kanan  = (abs($kanan_R - 1) + abs($kanan_S - 1) + abs($kanan_T - 1)) / 3;
                    
                    $penyulang->ketidakseimbangan_sumber    = $ketidakseimbangan_sumber * 100;
                    $penyulang->ketidakseimbangan_kiri      = $ketidakseimbangan_kiri * 100;
                    $penyulang->ketidakseimbangan_kanan     = $ketidakseimbangan_kanan * 100;
                    
                    $penyulang->save();
                                        
                }
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode);
    }
    
    public function postAddlocations() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Add location table and data success.";
        $isError          = FALSE;
        $missingParams    = null;

        if(!$isError){
            try {
                Schema::dropIfExists('locations');
                
                Schema::create('locations', function($table){
                    $table->increments('id');
                    $table->string('area');
                    $table->string('rayon');
                    $table->string('penyulang');
                });
                   
                $data = array('Wuawua' => array('Balaikota', 'Kendari Beach', 'Batu gong', 'Konda', 'Mata', 'Airport', 'Gubernur', 'Andonouhu', 'PPS', 'Lapuko', 'Crossing Teluk', 'Pohara', 'Ahmad Dahlan', 'Ekspress 1', 'Ekspress 2', 'Ambeipua Baru', 'RRI', 'Tobuha'),
                               'Benubenua' => array('Kendari TV','Mandonga','L.L. Meeto','Toronipa','Crossing Teluk','Ekspress 3'),
                               'Konawe Selatan' => array('Ambeipua','Konda','Lapuko'),
                               'Unaaha' => array('Raterate','Unaaha','Motaha','Lambuya'),
                               'Kolaka' => array('Wondulako','Pendidikan','Toari','Toshiba'),
                               'Kolaka Utara' => array('Utara', 'Selatan'),
                               'Bombana' => array('Kasepute', 'Bambaeya', 'Bombana Tengah')
                              );
             
                $area = 'Kendari';
                
                foreach ($data as $rayon => $value) {
                    foreach ($value as $penyulang) {
                        $location = new Locations;
                        
                        $location->area         = $area;
                        $location->rayon        = $rayon;
                        $location->penyulang    = $penyulang;
                            
                        $location->save();
                    }
                }
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode);
    }
    
    public function postAddlocationcolumn() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Add location columns in table gangguan and penyulang success.";
        $isError          = FALSE;
        $missingParams    = null;

        if(!$isError){
            try {
                Schema::table('gangguan', function($table)
                {
                    $table->string('area');
                    $table->string('rayon');
                    $table->string('feeder');
                });
                
                Schema::table('penyulang', function($table)
                {
                    $table->string('area');
                    $table->string('rayon');
                });
                
                
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode);   
    }
    
    public function postAddlocationdummy() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Add location in table gangguan success.";
        $isError          = FALSE;
        $missingParams    = null;

        if(!$isError){
            try {
                foreach (Gangguan::all() as $gangguan) {
                    $location = Locations::find(rand(1,40));
                    
                    $gangguan->area     = $location->area;
                    $gangguan->rayon    = $location->rayon;
                    $gangguan->feeder   = $location->penyulang;

                    $gangguan->save();
                }
                
                foreach (Penyulang::all() as $penyulang) {
                    $location = Locations::find(rand(1,40));
                    
                    $penyulang->area     = $location->area;
                    $penyulang->rayon    = $location->rayon;
                    $penyulang->feeder   = $location->penyulang;

                    $penyulang->save();
                }
                
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }
    }
    
    public function postMuthafaka() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "another configuration success.";
        $isError          = FALSE;
        $missingParams    = null;

        if(!$isError){
            try {
                Schema::table('penyulang', function($table)
                {
                    $table->decimal('kesetimbangan_kiri', 10, 2);
                    $table->decimal('kesetimbangan_kanan', 10, 2);
                    $table->decimal('inetral_kiri', 10, 2);
                    $table->decimal('inetral_kanan', 10, 2);
                });
                
                foreach (Penyulang::all() as $penyulang) {
                    $arus_R_kiri    = $penyulang->datapengukuran_arus_R_kiri;
                    $arus_S_kiri    = $penyulang->datapengukuran_arus_S_kiri;
                    $arus_T_kiri    = $penyulang->datapengukuran_arus_T_kiri;
                    $arus_N_kiri    = $penyulang->datapengukuran_arus_N_kiri;
                    $arus_R_kanan   = $penyulang->datapengukuran_arus_R_kanan;
                    $arus_S_kanan   = $penyulang->datapengukuran_arus_S_kanan;
                    $arus_T_kanan   = $penyulang->datapengukuran_arus_T_kanan;
                    $arus_N_kanan   = $penyulang->datapengukuran_arus_N_kanan;

                    $some_kiri                      = array($arus_R_kiri,   $arus_S_kiri,   $arus_T_kiri);
                    $some_kanan                     = array($arus_R_kanan,  $arus_S_kanan,  $arus_T_kanan);
                    $kesetimbangan_kiri             = floatval(number_format((float)(((max($some_kiri) - min($some_kiri)) / (array_sum($some_kiri) / 3)) * 100), 2, '.', ''));
                    $kesetimbangan_kanan            = floatval(number_format((float)(((max($some_kanan) - min($some_kanan)) / (array_sum($some_kanan) / 3)) * 100), 2, '.', ''));
                    $inetral_kiri                   = floatval(number_format((float)(($arus_N_kiri / (array_sum($some_kiri) / 3)) * 100), 2, '.', ''));
                    $inetral_kanan                  = floatval(number_format((float)(($arus_N_kanan / (array_sum($some_kanan) / 3)) * 100), 2, '.', ''));
                    $penyulang->inetral_kiri                        = $inetral_kiri;
                    $penyulang->inetral_kanan                       = $inetral_kanan;
                    $penyulang->kesetimbangan_kiri                  = $kesetimbangan_kiri;
                    $penyulang->kesetimbangan_kanan                 = $kesetimbangan_kanan;
                    
                    $penyulang->save();
                }
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }
        
        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode);   
    }
    
    public function missingMethod($parameters = array()) {
        echo 'Whooops!';
    }
}

?>
